package com.ecreditpal.audio.util;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

/**
 * @author lifeng
 * @version 1.0 on 2018/7/5.
 */
public class BaseUtil {
    private static String hexStr = "0123456789ABCDEF";  //全局

    public static int bytes2Int(byte[] bytes, int start, int end) {
        byte[] b = new byte[4];

        System.arraycopy(bytes, start, b, 0, end - start + 1);

        int value;
        value = (int) ((b[0] & 0xFF)
                | ((b[1] & 0xFF) << 8)
                | ((b[2] & 0xFF) << 16)
                | ((b[3] & 0xFF) << 24));
        return value;

    }


    public static int bytes2IntReverse(byte[] bytes, int start, int end) {
        byte[] b = new byte[4];

        System.arraycopy(bytes, start, b, 0, end - start + 1);

        int value;
        value = (int) ((b[0] & 0xFF) << 24
                | ((b[1] & 0xFF) << 16)
                | ((b[2] & 0xFF) << 8)
                | ((b[3] & 0xFF)));
        return value;

    }


    public static String bytes2String(byte[] bytes, int start, int end) {
        byte[] b = new byte[end - start + 1];

        System.arraycopy(bytes, start, b, 0, end - start + 1);

        return new String(b);
    }

    public static byte[] intToBytes(int value) {
        byte[] src = new byte[4];
        src[3] = (byte) ((value >> 24) & 0xFF);
        src[2] = (byte) ((value >> 16) & 0xFF);
        src[1] = (byte) ((value >> 8) & 0xFF);
        src[0] = (byte) (value & 0xFF);
        return src;
    }


    public static byte[] intToBytesReverse(int value) {
        byte[] src = new byte[4];
        src[3] = (byte) (value & 0xFF);
        src[2] = (byte) ((value >> 8) & 0xFF);
        src[1] = (byte) ((value >> 16) & 0xFF);
        src[0] = (byte) ((value >> 24) & 0xFF);
        return src;
    }


    /**
     * @param s      参数URLSubServiceType
     * @param key    secretKey密匙
     * @param method 加密方法
     * @return 加密串
     */
    public static String sign(String s, String key, String method, String charset) {
        Mac mac = null;
        try {
            mac = Mac.getInstance(method);

            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(charset), mac.getAlgorithm());
            mac.init(secretKeySpec);
            byte[] hash = mac.doFinal(s.getBytes(charset));
            return DatatypeConverter.printBase64Binary(hash);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    public static byte[] mergeBytes(byte[] a, byte[] b) {
        byte[] newBytes = new byte[a.length + b.length];
        System.arraycopy(a, 0, newBytes, 0, a.length);
        System.arraycopy(b, 0, newBytes, a.length, b.length);
        return newBytes;
    }

    public static String byteToHex(byte a) {
        //字节高4位
        String hex = String.valueOf(hexStr.charAt((a & 0xF0) >> 4));
        //字节低4位
        hex += String.valueOf(hexStr.charAt(a & 0x0F));

        return "0x" + hex;
    }

    public static void main(String[] args) {
        System.out.println(byteToHex((byte) 2));
    }
}
